package com.stgy.lease.web.admin.controller.system;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stgy.lease.common.exception.StgyException;
import com.stgy.lease.common.result.Result;
import com.stgy.lease.common.result.ResultCodeEnum;
import com.stgy.lease.model.domain.SystemUser;
import com.stgy.lease.model.domain.UserInfo;
import com.stgy.lease.web.admin.dto.BaseSearchDto;
import com.stgy.lease.web.admin.dto.SystemUserSearchDto;
import com.stgy.lease.web.admin.service.SystemUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName:SystemUserController
 * Package:com.stgy.lease.web.admin.controller.system
 * Description:
 *
 * @Author:helei
 * @Create:2024/9/3 - 15:17
 */
@RestController
@Tag(name = "后台用户信息管理")
@RequestMapping("/admin/system/user")
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;

    //http://localhost:5174/admin/system/user/page?current=1&size=10&name=%E5%BC%A0%E4%B8%89
    // &phone=10086 GET
    @Operation(summary = "条件分页查询管理员列表")
    @GetMapping("/page")
    public Result<Page<SystemUser>> page(SystemUserSearchDto searchDto){
        return Result.ok(systemUserService.getPage(searchDto));
    }


    //http://localhost:5174/admin/system/user/isUserNameAvailable?username=zhangsan123 GET
    @Operation(summary = "校验用户名是否存在")
    @GetMapping("/isUserNameAvailable")
    public Result<Boolean> isUserNameAvailable(@RequestParam String username){
        return Result.ok(systemUserService.exists(username));
    }

    @Operation(summary = "新增或更新用户")
    @PostMapping("/saveOrUpdate")
    public Result<SystemUser> saveOrUpdate(@RequestBody SystemUser systemUser){

        return Result.build(systemUserService.saveOrUpdateSystemUser(systemUser));
    }

    //http://localhost:5174/admin/system/user/updateStatusByUserId?id=1&status=0 POST
    @Operation(summary = "根据id更新用户状态")
    @PostMapping("/updateStatusByUserId")
    public Result<String> updateStatusByUserId(@RequestParam Long id,@RequestParam Integer status){
        return Result.build(systemUserService.update(Wrappers.lambdaUpdate(SystemUser.class)
                .ge(SystemUser::getId, id)
                .set(SystemUser::getStatus, status)));
    }

    //http://localhost:5174/admin/system/user/deleteById?id=1830879290053406721 DELETE
    @Operation(summary = "根据id删除用户")
    @DeleteMapping("/deleteById")
    public Result<String> deleteById(@RequestParam Long id){
        return Result.build(systemUserService.removeById(id));
    }
}
